<?php
	/**
	 * MYSQL CLASSS
	 * @author Tom
	 *
	 */
	include_once(dirname(__FILE__).'/../config/DB.config.php');
	class MySql
	{
		private $connection;
		private $query;
		//Connect to mysql and select database
		function __construct($query = '')
		{
			$this->connection = mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD);
			if(!$this->connection)
			die("Can't connect to MySql");
			mysql_select_db(DB_NAME) or die("Can't select database");
			if($query!='') $this->setQuery($query);
		}
		//Set query
		public function setQuery($query)
		{
			$this->query = $query;
		}
		//Get query
		public function getQuery()
		{
			return $this->query;
		}
		//Run a query
		public function query($query = '')
		{
			if ($query == '') $query = $this->query;
			return mysql_query($query);
		}
		//Return num of rows
		public function numRows($query = '')
		{
			if ($query == '') $query = $this->query;
			return mysql_num_rows($this->query($query));
		}
		public function singleArray($query = '')
		{
			if ($query == '') $query = $this->query;
			$result = $this->query($query);
			return mysql_fetch_array($result,MYSQL_ASSOC);
		}
		//Get data from MySql and asign to Array
		public function fetchArray($query = '')
		{
			if ($query == '') $query = $this->query;
			$result = $this->query($query);
			while($row = mysql_fetch_array($result,MYSQL_ASSOC))
			{
				$rows[] = $row;
			}
			mysql_free_result($result);
			return $rows;
		}
		//return true/false for checking data
		public function checkData($query = '')
		{
			if ($query == '') $query = $this->query;
			if($this->numRows($query)!= 0)
			{
				return true;
			}
				return false;
		}

		//insert data. Return true if ok, false if not ok
		public function insert($table,$list)
		{
			if(!is_array($list))
			{
				$data = explode(',',$list);
				foreach($data as $val)
				{
					$ldata.="'".$val."',";
				}
				$ldata = substr($ldata, 0, strlen($ldata)-1);
				$query = "INSERT INTO ".$table." VALUES(".$ldata.")";
			}
			else
			{
				foreach($list as $key => $val)
				{
					$lkey.=$key.",";
					$ldata.="'".$val."',";
				}
				$lkey = substr($lkey, 0, strlen($lkey)-1);
				$ldata = substr($ldata, 0, strlen($ldata)-1);
				$query = "INSERT INTO ".$table."(".$lkey.") VALUES(".$ldata.")";
			}
			return $this->query($query);
		}

		//update data. Return true if ok, false if not ok
		public function update($table, $list, $condition = false)
		{
			foreach($list as $key => $val)
			{
				$lset .= $key."='".$val."',";
			}
			$lset = substr($lset, 0, strlen($lset)-1);
			$query = "UPDATE ".$table." SET ".$lset."";
			if($condition)
			{
				$query .= " WHERE ".$condition;
			}
			return $this->query($query);
		}

		//delete data. Return true if ok, false if not ok
		public function delete($table, $condition = false)
		{
			$query = "DELETE FROM ".$table;
			if($condition)
			{
				$query .= " WHERE ".$condition;
			}
			return $this->query($query);
		}
		public function arrayLimit($start, $quantity)
		{
			$query = ($this->query)." LIMIT ".$start.", ".$quantity;
			$result = $this->query($query);
			while($row = mysql_fetch_array($result,MYSQL_ASSOC))
			{
				$rows[] =  $row;
			}
			mysql_free_result($result);
			return $rows;
		}
////////////////////////////////////////////////////////////////////////////////////////////////////////
		//Fill query select to show data in database
		public function selectFillSql($query = '')
		{
			if(empty($query)) { return false; }
			if(!eregi("^select",$query))
			{
				echo "Wrong Query<hr>$query<p>";
				return false;
			}
			$result = $this->query($query);
			if( (!($result)) or (empty($result)) )
			{
				return false;
			}
			while ($row = mysql_fetch_array($result))
			{
				$rows[] = $row;
			}
			mysql_free_result($result);
			return $rows;
		}
		//Fill query insert to insert data in database
		function insertFillSql($query = '')
		{
			if(empty($query)) { return false; }
			if(!eregi("^insert",$query))
			{
				return false;
			}
			$result = $this->query($query);
			if(!$result)
			{
				return false;
			}
			$id = mysql_insert_id();
			mysql_free_result($result);
			return $id;
		}
		//Fill query edit to execute database and update data
		function updateFillSql($query = '')
		{
			if(empty($query)) { return false; }
			if(!eregi("^update",$query))
			{
				return false;
			}
			$result = $this->query($query);
			if(!$result)
			{
				return false;
			}
			$rows = 0;
			$rows = mysql_affected_rows();
			mysql_free_result($result);
			return $rows;
		}
		//Check that the data has been interact with the database or not ?
		function affectedFillSql($query ='')
		{
			if(empty($query)) { return false; }
			if(!eregi("^select",$query))
			{
				return false;
			}
			$result = $this->query($query);
			if( (!$result) or (empty($result)) )
			{
				return false;
			}
			$tot=0;
			$tot=mysql_affected_rows();
			mysql_free_result($result);
			return $tot;
		}
		//Check error
		public function error($text)
		{
			$no = mysql_errno();
			$msg = mysql_error();
			exit;
		}
		function __destruct(){
			$this->close();
		}
		public function close()
		{
			@mysql_close($this->connection);
		}
	}
?>
